import java.awt.*;
import java.beans.*;
import java.io.*;

public class Map extends java.awt.Panel implements Serializable
{
	int w = 20;
	int h = 20;
	transient Image buffer; 

	
	GridCell gridCell[][] = new GridCell[w][h];
	public Map()
	{
		super();
		//{{INIT_CONTROLS
		setLayout(new GridLayout(w,h));
		setSize(insets().left + insets().right + 430,insets().top + insets().bottom + 270);
		//}}
		for(int i=0;i<w;i++){
			for(int j=0;j<h;j++){
				gridCell[j][i] = new GridCell();
				gridCell[j][i].setPosition(new Point(j,i));
				add(gridCell[j][i]);
			}
		}
	}





	public void paint(Graphics g){
		if(buffer == null){buffer = createImage(getBounds().width,getBounds().height);}
		Graphics bg = buffer.getGraphics();
		super.paint(bg);
		bg.setColor(Color.black);
		g.drawImage(buffer,0,0,null);
		//g.drawRect(0,0,getBounds().width-1,getBounds().height-1);
	}

	public void update(Graphics g){
		paint(g);
	}

	public Point getStartPosition(){
		GridCell start = GridCell.getStartCell();
		return start.getPosition();
	}

	public Point getFinishPosition(){
		GridCell finish = GridCell.getFinishCell();
		return finish.getPosition();
	}
	
	
	public GridCell[] getAdjacent(GridCell g){
		GridCell next[] = new GridCell[9];//Extend to 8 directions
		Point p = g.getPosition();	
			
		
		if(p.y!=0){next[0]=gridCell[p.x][p.y-1];}
		if(p.x!=w-1){next[1]=gridCell[p.x+1][p.y];}
		if(p.y!=h-1){next[2]=gridCell[p.x][p.y+1];}
		if(p.x!=0){next[3]=gridCell[p.x-1][p.y];}

		if(p.x!=w-1&&p.y!=0){next[4]=gridCell[p.x+1][p.y-1];}//Add diagonal cells
		if(p.x!=w-1&&p.y!=h-1){next[5]=gridCell[p.x+1][p.y+1];}
		if(p.x!=0&&p.y!=h-1){next[6]=gridCell[p.x-1][p.y+1];}
		if(p.x!=0&&p.y!=0){next[7]=gridCell[p.x-1][p.y-1];}

		GridCell shadowCell=new GridCell();
		shadowCell.setPosition(p);	
		next[8]=shadowCell;

		return next;
	}

	/*public GridCell getLowestAdjacent(GridCell g){
		GridCell next[] = getAdjacent(g);
		GridCell small = next[0];
		double dist = Double.MAX_VALUE;
		for(int i=0;i<8;i++){//Extend to 8
			if(next[i]!=null){

				//Cannot go across corner.

				if (i==4&&(next[0].isTotalBlock()||next[1].isTotalBlock())) continue;
				if (i==5&&(next[1].isTotalBlock()||next[2].isTotalBlock())) continue;
				if (i==6&&(next[2].isTotalBlock()||next[3].isTotalBlock())) continue;
				if (i==7&&(next[3].isTotalBlock()||next[4].isTotalBlock())) continue;


				double nextDist = next[i].getDistFromStart();
				if(nextDist<dist && nextDist>=0){
					small = next[i];
					dist = next[i].getDistFromStart();
				}
			}
		}
		return small;
	}
*/
	private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException{
		GridCell.tidy=false;
		ois.defaultReadObject();
		GridCell.setShowPath(false);
	}
	//{{REGISTER_LISTENERS
	//};
	//{{DECLARE_CONTROLS
	//}}





	public void resetVelocity() {
				
		
		
		
		
		
		
	}





	public GridCell getGridCell(Point position) {
	int Px=(int) position.getX();
	int Py=(int) position.getY();
	
	return this.gridCell[Px][Py];		
	}
		

}